package com.jrx.anytxn.param.controller.product;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.product.PrBlockCodeReq;
import com.jrx.anytxn.param.dto.product.PrBlockCodeRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.product.IPrBlockCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @author liuyu
 * @Description
 */
@Api(tags = "页面封锁码表参数API接口")
@RestController
@RequestMapping(value = "/param/blockCode")
public class PrBlockCodeController {

    private static final Logger logger = LoggerFactory.getLogger(PrBlockCodeController.class);

    @Autowired
    private InfoService infoService;
    @Autowired
    private IPrBlockCodeService prBlockCodeService;

    @ApiOperation(value = "根据租户ID分页查询封锁码表信息", notes = "根据租户ID分页查询封锁码表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true),})
    public TxnRespResult<TxnPage<PrBlockCodeRes>> getAllPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<PrBlockCodeRes>> result = new TxnRespResult<>();
        TxnPage<PrBlockCodeRes> res;
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            res = prBlockCodeService.findAllPageByTenantId(pageNum, pageSize, tenantId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和封锁码查询封锁码表信息", notes = "根据租户ID和封锁码查询封锁码表信息")
        @GetMapping(value = "/tenantId/{tenantId}/blockCode/{blockCode}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "blockCode", value = "封锁码", required = true)
    })
    public TxnRespResult<PrBlockCodeRes> getByTenIdAndBlockCode(@NotNull @PathVariable String tenantId, @PathVariable String blockCode) {
        TxnRespResult<PrBlockCodeRes> result = new TxnRespResult<>();
        PrBlockCodeRes res;
        try {
            res = prBlockCodeService.findByTenIdAndBlockCode(String.valueOf(infoService.getUser().getProjectId()), blockCode);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }



    @ApiOperation(value = "新增封锁码表信息", notes = "新增封锁码表信息")
    @PostMapping("/")
    public TxnRespResult<PrBlockCodeRes> createPrBlockCode(@RequestBody PrBlockCodeReq prBlockCodeReq) {
        PrBlockCodeRes res;
        TxnRespResult<PrBlockCodeRes> result = new TxnRespResult<>();
        try {
            prBlockCodeReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prBlockCodeReq.setUpdateBy(infoService.getUser().getUserName());
            res = prBlockCodeService.addPrBlockCode(prBlockCodeReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "编辑封锁码表信息", notes = "编辑封锁码表信息")
    @PutMapping("/")
    public TxnRespResult<PrBlockCodeRes> modifyPrBlockCode(@RequestBody PrBlockCodeReq prBlockCodeReq) {
        TxnRespResult<PrBlockCodeRes> result = new TxnRespResult<>();
        PrBlockCodeRes res;
        try {
            prBlockCodeReq.setUpdateBy(infoService.getUser().getUserName());
            res = prBlockCodeService.modifyPrBlockCode(prBlockCodeReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation("根据租户ID和封锁码删除此信息")
    @DeleteMapping("/{tenantId}/{organizationId}/{blockCode}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "organizationId", value = "机构ID", required = true),
            @ApiImplicitParam(name = "blockCode", value = "还款方式", required = true)
    })
    public TxnRespResult<Boolean> removeById(@PathVariable String tenantId, @PathVariable String organizationId,
                                             @PathVariable String blockCode) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = prBlockCodeService.removeByTenIdAndBlockCode(tenantId, organizationId,blockCode);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }


}
